/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hymane.etvexample.slice;

import com.hymane.etvexample.ResourceTable;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
/**
 * InputDialog
 *
 * @author wz
 * @since 2021-06-08
*/
public class InputDialog extends CommonDialog {
    private TextField textField;
    private Text mCandle;
    private Text mOk;
    private String content;
    private Listener listener;

    /**
      * InputDialog
      *
      * @param context context
      *
      * */
    public InputDialog(Context context) {
        super(context);
        init(context);
    }
    public Listener getListener() {
        return listener;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    /**
     * getContent
     *
     * @return content
     */
    public String getContent() {
        content = textField.getText().toString();
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    private void init(Context context) {
        this.setTransparent(true);
        InputView inputView = new InputView(context);
        setContentCustomComponent(inputView);
        textField = (TextField) inputView.findComponentById(ResourceTable.Id_text);
        mCandle = (Text) inputView.findComponentById(ResourceTable.Id_candle);
        mOk = (Text) inputView.findComponentById(ResourceTable.Id_ok);
        mCandle.setClickedListener(component -> listener.getLeftListener());
        mOk.setClickedListener(component -> listener.getRightListener());
    }
    /**
     * Listener
     *
     * @author wz
     * @since 2021-06-08
     */

    public interface Listener {
        /**
         * getLeftListener
         */
        void getLeftListener();

        /**
         * getRightListener
         */
        void getRightListener();
    }
    /**
     * InputView
     *
     * @author wz
     * @since 2021-06-08
     */
    static class InputView extends DirectionalLayout {
        InputView(Context context) {
            super(context);
            setupComponent(context);
        }

        private void setupComponent(Context context) {
            LayoutScatter.getInstance(context).parse(ResourceTable.Layout_input, this, true);
        }
    }
}
